/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.configurator.ui;

import dale2507.gates.configurator.ui.components.BlockSelector;
import dale2507.gates.configurator.ui.components.MaterialSelector;
import dale2507.gates.data.Settings;
import dale2507.gates.data.Settings.GateCreationSettings;
import javax.swing.JCheckBox;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import org.bukkit.Material;

/**
 *
 * @author Dale
 */
class CreationSettings extends PropertiesPage {
    
    private BlockSelector gateMaterial = new BlockSelector();
    private BlockSelector defaultChevron = new BlockSelector(new Material[]{Material.TORCH,Material.REDSTONE_TORCH_ON});
    private JSpinner minChevrons = new JSpinner();
    private JCheckBox outgoingOnly = new JCheckBox();
    private JCheckBox autoDial = new JCheckBox();
    private MaterialSelector indefiniteMaterial = new MaterialSelector(Material.values());
    private JCheckBox fixOnLoad = new JCheckBox();
    
    CreationSettings() {
        super();
        
        this.addLabelComponentPair("Material", gateMaterial, "The block which gates should be made of.");
        this.addLabelComponentPair("Default Chevron Torch", defaultChevron, "The default torch to be used when gates are dialled.");
        // TODO search area
        SpinnerNumberModel snm = new SpinnerNumberModel(3,1,9,1);
        minChevrons.setModel(snm);
        minChevrons.setEditor(new JSpinner.NumberEditor(minChevrons));
        this.addLabelComponentPair("Minimum Chevrons", minChevrons, "The minimum number of items which can be used in a gate address.");
        this.addLabelComponentPair("Allow Outgoing Only", outgoingOnly, "Allow gates to be outgoing only so players cannot dial in.");
        this.addLabelComponentPair("Allow Auto Dial", autoDial, "Allow gates to dial a specific gate without placing anything in the chest.");
        this.addLabelComponentPair("Indefinite Item", indefiniteMaterial, "The item used to make a gate keep a connection indefinitely.");
        this.addLabelComponentPair("Fix on Load", fixOnLoad, "Attempts to fix dammaged gates when they are loaded");
        
        if (Configurator.isLive()) {
            liveLoad();
        } else {
            // TODO 
        }
    }

    private void liveLoad() {
        GateCreationSettings s = Settings.getInstance().creation;
        gateMaterial.setSelectedItem(s.getGateMaterial());
        defaultChevron.setSelectedItem(s.defaultChevronTorch());
        minChevrons.getModel().setValue(s.minimumChevrons());
        outgoingOnly.setSelected(s.allowOutgoingOnly());
        autoDial.setSelected(s.allowAutoDial());
        indefiniteMaterial.setSelectedItem(s.indefiniteConnectionItem());
        fixOnLoad.setSelected(s.fixOnLoad());
    }
    
    @Override
    void liveSave() {
        GateCreationSettings s = Settings.getInstance().creation;
        s.setGateMaterial((Material) gateMaterial.getSelectedItem());
        s.setDefaultChevronTorch((Material) defaultChevron.getSelectedItem());
        s.setMinimumChevrons((int) minChevrons.getValue());
        s.setOutgoingOnly(outgoingOnly.isSelected());
        s.setAutoDial(autoDial.isSelected());
        s.setIndefiniteConnectionItem((Material) indefiniteMaterial.getSelectedItem());
        s.setFixOnLoad(fixOnLoad.isSelected());
    }

    @Override
    String toYaml() {
        return "# Settings for creating gates\r\n" +
            "gate_creation:\r\n" +
            "  \r\n" +
            "  # Material used for creating gates\r\n" +
            "  # default = 49 (Obisidian)\r\n" +
            "  gate_material: " + gateMaterial.getTypeId() + "\r\n" +
            "  \r\n" +
            "  # Default torch used for chevrons\r\n" +
            "  # Possible values: 0 (None), 50 (Torch), 76 (Redstone torch)\r\n" +
            "  # Default: 76 (Redstone Torch)\r\n" +
            "  default_chevron: " + defaultChevron.getTypeId() + "\r\n" +
            "  \r\n" +
            "  # Area used to search for gate componenets\r\n" +
            "  # Default: 20x10\r\n" +
            "  search_area:\r\n" +
            "    horizontal: 20\r\n" +
            "    vertical: 10\r\n" +
            "  \r\n" +
            "  # Minimum number of item stacks for an address\r\n" +
            "  # Possible values: 1-9\r\n" +
            "  # Default: 3\r\n" +
            "  # Use 7 for stargate theme\r\n" +
            "  minimum_chevrons: " + String.valueOf(minChevrons.getModel().getValue()) + "\r\n" +
            "  \r\n" +
            "  # Allow outgoing only gates\r\n" +
            "  # Default: true\r\n" +
            "  outgoing_only: " + outgoingOnly.isSelected() + "\r\n" +
            "  \r\n" +
            "  # Allow auto dail\r\n" +
            "  # Default: true\r\n" +
            "  auto_dial: " + autoDial.isSelected() + "\r\n" +
            "  \r\n" +
            "  # Indefinite connection item\r\n" +
            "  # Item to be placed in DHD (Chest for indefinite connections)\r\n" +
            "  # Default: 0 (Disabled)\r\n" +
            "  indefinite_item: " + indefiniteMaterial.getTypeId() + "\r\n";
    }
    
}
